//
//  SYProvider.swift
//  Entrance
//
//  Created by chunhuiLai on 2018/1/11.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import SYKit
import Moya
import SwiftyJSON

enum SYProvider {
    case infinity
    case entrance
    
    var provider: MoyaProvider<MultiTarget> {
        switch self {
        case .infinity:
            return MoyaProvider(endpointClosure: { (target) -> Endpoint<MultiTarget> in
                return Endpoint(
                    url: URL(target: target).absoluteString,
                    sampleResponseClosure: { .networkResponse(200, target.sampleData) },
                    method: target.method,
                    task: target.task,
                    httpHeaderFields: target.headers
                )
            },  requestClosure: commonRequestClosure)
            
        case .entrance:
            return MoyaProvider(endpointClosure: { (target) -> Endpoint<MultiTarget> in
                return Endpoint(
                    url: URL(target: target).absoluteString,
                    sampleResponseClosure: { .networkResponse(200, target.sampleData) },
                    method: target.method,
                    task: target.task,
                    httpHeaderFields: target.headers
                )
            },  requestClosure: commonRequestClosure)
   
        }
    }
}

extension MoyaProvider {
    
    /// 封装公共参数
    final public class func sy_packageRequestParams(_ params: JSONDictionary?) -> [String: Any] {
        
        var tempDic: [String: Any] = [:]
        tempDic["Ip"] = ""
        tempDic["SystemName"] = UIDevice.sy_systemVersion()
        tempDic["Version"] = SYApplicationInfo.appVersion()
        tempDic["Token"] = "ddddddd"
        tempDic["UserId"] =  ""
      
        tempDic["Channel"] = "AppStore"
        tempDic["MachineCode"] = ""
        
       // let interval = TDUserDefaultsManager.getServerAndSystemTimeInterval()
        // let serverDate = Date(timeInterval: interval, since: Date())
       // let time = serverDate.timeIntervalSince1970 * 1000 //单位为毫秒。
//        let str = "\(time)".tdw_separatBy(".").first
//        // 转整型
//        if let str = str {
//            tempDic["RequestTime"] = str
//        }else{
//            tempDic["RequestTime"] = time
//        }
        if let params = params {
            let json = JSON(params)
            let jsonString = json.rawString(String.Encoding.utf8, options: .prettyPrinted)
            tempDic["Data"] = jsonString
        }
        
        let uuidString = NSUUID().uuidString
        var resultDic: [String: Any] = [:]
        resultDic["UUId"] = uuidString
    
//        let tempString = JSON(tempDic).rawString(String.Encoding.utf8, options: .prettyPrinted)
//        if let tempstr = tempString {
//            let encryptStr = TDWSecurityManager.shared.encrypt(forContent: tempstr, key: uuidString)
//            resultDic["Param"] = encryptStr
//        }
        return resultDic
    }
    
}


/// 设置请求超时时间
let commonRequestClosure =  { (endpoint: Endpoint<MultiTarget>, done: @escaping MoyaProvider<MultiTarget>.RequestResultClosure) in
    guard var request = try? endpoint.urlRequest() else { return }
    
    request.timeoutInterval = 10
    done(.success(request))
}
